package com.Dao;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.sql.Statement;
import com.pojo.Sight;
import com.pojo.Sub;
import com.util.SqlUtil;
public class ProcDao {
	private int Count;
	private int Count1;
	static {// 导入MySql驱动包
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
		}
	}

	private Connection getConn() throws SQLException {
		String url = "jdbc:mysql://localhost:3306/hi_db" + "?useUnicode=true&characterEncoding=UTF-8";
		String username = "root";
		String password = "15723";
		Connection conn = DriverManager.getConnection(url, username, password);
		return conn;
	}

	public Integer save(Integer UId,Integer SId, String huifu) throws SQLException {
		// 1.创建连接
		Connection conn = getConn();// 调用上面的方法
		// 2.创建执行块
		Statement st = conn.createStatement();
		// 3.编写SQL代码
		String sql = "INSERT INTO huifu_table(UId,SId,huifu,times) "
				+ "VALUES("+UId+","+SId+",'"+huifu+"',now())";
		Integer result = st.executeUpdate(sql);
		// 5、关闭执行块
		st.close();
		// 6、关闭连接
		conn.close();
		// 7、返回结果
		return result;
	}
	
	public Integer savex(Integer UId,String title,Integer count, String contant) throws SQLException {
		// 1.创建连接
		Connection conn = getConn();// 调用上面的方法
		// 2.创建执行块
		Statement st = conn.createStatement();
		// 3.编写SQL代码
		String sql = "INSERT INTO cards(UId,Tid,contant,img,time,title) "
				+ "VALUES('"+UId+"',"+count+",'"+contant+"','img1.jpg',now(),'"+title +"')";
		Integer result = st.executeUpdate(sql);
		// 5、关闭执行块
		st.close();
		// 6、关闭连接
		conn.close();
		// 7、返回结果
		return result;
	}
	public void copy(File file, File toFile) throws Exception {
		byte[] b = new byte[1024];
		int a;
		FileInputStream fis;
		FileOutputStream fos;
		if (file.isDirectory()) {
			String filepath = file.getAbsolutePath();
			filepath=filepath.replaceAll("\\\\", "/");
			String toFilepath = toFile.getAbsolutePath();
			toFilepath=toFilepath.replaceAll("\\\\", "/");
			int lastIndexOf = filepath.lastIndexOf("/");
			toFilepath = toFilepath + filepath.substring(lastIndexOf ,filepath.length());
			File copy=new File(toFilepath);
			//复制文件夹
			if (!copy.exists()) {
				copy.mkdir();
			}
			//遍历文件夹
			for (File f : file.listFiles()) {
				copy(f, copy);
			}
		} else {
			if (toFile.isDirectory()) {
				String filepath = file.getAbsolutePath();
				filepath=filepath.replaceAll("\\\\", "/");
				String toFilepath = toFile.getAbsolutePath();
				toFilepath=toFilepath.replaceAll("\\\\", "/");
				int lastIndexOf = filepath.lastIndexOf("/");
				toFilepath = toFilepath + filepath.substring(lastIndexOf ,filepath.length());
				
				//写文件
				File newFile = new File(toFilepath);
				fis = new FileInputStream(file);
				fos = new FileOutputStream(newFile);
				while ((a = fis.read(b)) != -1) {
					fos.write(b, 0, a);
				}
			} else {
				//写文件
				fis = new FileInputStream(file);
				fos = new FileOutputStream(toFile);
				while ((a = fis.read(b)) != -1) {
					fos.write(b, 0, a);
				}
			}
 
		}
	}

	public Integer savetheme( String them) throws SQLException {
		// 1.创建连接
		Connection conn = getConn();// 调用上面的方法
		// 2.创建执行块
		Statement st = conn.createStatement();
		// 3.编写SQL代码
		String sql1 = "select Tid from theme_table where theme='"+them+"'";
		
		
		ResultSet rs = st.executeQuery(sql1);
		if(rs.next()) {//为true代表有数据
			Integer pasd =rs.getInt("Tid");
					
			rs.close();//关闭结果集
			st.close();//关闭执行块
			conn.close();//关闭连接
			return pasd;//返回查询到的用户信息
		}
		else {
			rs.close();//关闭结果集
			st.close();//关闭执行块
			conn.close();//关闭连接
			return null;
		}
	}

	
	public Sight[] findAllGart(Integer sId) throws SQLException {
		//1.创建连接
		Connection conn = getConn();//调用上面的方法
		//2.创建执行块
		Statement st = conn.createStatement();
		//SQL
		String sql = "SELECT huifu_table.UId as UId,huifu,Uname,times from huifu_table"
				+ " left join user on huifu_table.UId=user.Uid where SId = '"+sId+"'";
				
		//执行
		ResultSet rs = st.executeQuery(sql);
		Sight[] shopArr = new Sight[100];
		this.Count = 0;//还原图书数量
		while(rs.next()) {
			Sight book = new Sight(
				rs.getInt("UId"),
				rs.getString("Uname"),
				rs.getString("huifu"),
				rs.getDate("times"));
			shopArr[Count] = book;
			Count++;
		}
		return shopArr;
	}

	public int getCount() {
		return Count;
	}
	
	
	public Sight[] findAllGartx(String sId) throws SQLException {
		//1.创建连接
		Connection conn = getConn();//调用上面的方法
		//2.创建执行块
		Statement st = conn.createStatement();
		//SQL
		String sql = "SELECT huifu_table.UId as UId,huifu,Uname,times from huifu_table"
				+ " left join user on huifu_table.UId=user.Uid"
				+ " where SId = '"+sId+"' order by times asc";
				
		//执行
		ResultSet rs = st.executeQuery(sql);
		Sight[] shopArrx = new Sight[100];
		this.Count1 = 0;//还原图书数量
		while(rs.next()) {
			Sight book = new Sight(
				rs.getInt("UId"),
				rs.getString("Uname"),
				rs.getString("huifu"),
				rs.getDate("times"));
			shopArrx[Count1] = book;
			Count1++;
		}
		return shopArrx;
	}

	public int getCount1() {
		return Count1;
	}
}
